#include <bits/stdc++.h>

using namespace std;
#define ll long long
const int mod = 1e9 + 7;
void solve()
{
    ll n, k;
    cin >> n >> k;
    ll res = 0, p = 1;
    while (k > 0) {
        if(k & 1)
        res = (res % mod + p % mod) % mod;
        p = (p * n) % mod;
        k = k >> 1;
    }
    cout << res % mod << "\n";
}
signed main() {
	int t = 1;
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}
